$(function(){
	initBlockUI();
	initFormValidate();
	initCheckUserName();
	initFormSubmit();
	initCalendar();
	initCompanySectionPosition();
});

// 初始化表单验证
function initFormValidate()
{
	$("#addperson").validate({
		event: "blur",
		rules: {
			username: { required: true, minLength: 2, maxLength: 8 },
			password: { required: true, minLength: 6, maxLength: 50 },
			repwd: { required: true, equalTo: "#password", minLength: 6, maxLength: 50 },
			job_num: { required: true },
			identity: { required: true, minLength: 16, maxLength: 18 },
			birth: { required: true, dateISO: true },
			tel: { minLength: 8, digits: true },
			phone: { minLength: 11, maxLength: 11, digits: true },
			qq: { minLength: 5, maxLength: 10, digits: true },
			email: { email: true },
			address: { required: true, maxLength: 100 },
			graduate: { required: true, dateISO: true },
			census_reg: { required: true }
		},
		messages: {
			username: {
				required: "请输入用户名！",
				minlength: "请输入至少 2 个字！",
				maxlength: "输入的字数不超过 8 个字！"
			},
			password: {
				required: "请输入密码！",
				minlength: "请输入至少 6 个字符！",
				maxlength: "输入的密码最多 50 个字符！"
			},
			repwd: {
				required: "请再输入一次密码！",
				equalTo: "重输密码与第一次输入不一致！",
				minlength: "请输入至少 6 个字符！",
				maxlength: "输入的密码最多 50 个字符！"
			},
			job_num: { required: "请输入员工工号！" },
			identity: { required: "请输入员工身份证号！", minlength: "身份证号码最少 16 位！", maxlength: "身份证号码最多 18 位！" },
			birth: { required: "请输入或选择员工的出生日期！", dateISO: "请输入正确的日期格式或从日期选择器中选择！" },
			tel: { minlength: "电话号码必须大于 8 位！", digits: "电话号码只允许输入数字！" },
			phone: { minlength: "请输入 11 位手机号码！", maxlength: "请输入 11 位手机号码！", digits: "手机号码只允许输入数字！" },
			qq: { minlength: "请输入至少 5 位的 QQ 号码！", maxlength: "请输入最多 10 位的 QQ 号码！", digits: "QQ 号码只允许输入数字！" },
			email: { email: "请输入正确的 Email 邮箱地址！" },
			address: { required: "请输入家庭地址！", maxlength: "家庭地址最多只能输入 100 个字！" },
			graduate: { required: "请输入毕业时间！", dateISO: "请输入正确的日期格式或从日期选择器中选择！" },
			census_reg: { required: "请输入户籍所在地！" }
		}
	});
}

// 初始化“检测是否可用”按钮
function initCheckUserName()
{
	$("#check_username").click(function(){
		var user = $("#username").val();
		if ($.trim(user) == "") { return false; }
		var url = "../xmls/check_username.php";
		var pars = { username: user };
		$.get(url, pars, function(text){
			if (/^Success/.test(text)) { window.alert("恭喜您，该用户名可用！"); }
			if (/^Error/.test(text)) { window.alert("对不起，该用户名已存在！请尝试在用户名后加上数字后缀（如“" + user + "2”）再检测。"); }
		});
		return false;
	});
}

// 初始化表单提交
function initFormSubmit()
{
	$("#addperson").ajaxForm({ success: function(text){
		if (/^Success/g.test(text))
		{
			window.alert("添加用户成功！");
			if (window.confirm("继续添加新用户？"))
			{
				// $("#addperson").resetForm();
				window.location.reload();
			}
			else
			{
				window.location = "main.php";
			}
		}
		else
		{
			window.alert(text);
		}
	}});
}

// 初始化日期表单
function initCalendar()
{
	$("#birth, #graduate").datepicker();
}

// 初始化公司部门职位选择
function initCompanySectionPosition()
{
	getCompanyListViaJs("#company_id", -1);
	$("#company_id").change(function(){
		getSectionListViaJs("#section_id", $(this).val(), -1);
		$("#position_id").html("");
	});
	$("#section_id").change(function(){
		getPositionListViaJs("#position_id", $(this).val(), -1);
	});
}
